<?php

  // Pour ajouter des équipes
  // auteur : sebastien Mosser
  
class sfEditAMatche extends aSubFeature {

   public function __construct() {
      $this->_acl = new AccessControlList();
      $this->_acl->setGranted(7);
      $this->_acl->setGranted(3);
      $this->_acl->setGranted(2);
   }

  protected function run() {
  	$action = PropRetriever::getProp($_POST,"action");
  	$id = PropRetriever::getProp($_GET,"id");
  	
		if ($id == "")
			$id = PropRetriever::getProp($_POST,"id");
		if ($id == "") {
			$ans = <<<EOS
<h1> Une erreur est survenue </h1>
<div align="center">
  <span class="error">
  	Vous devez spécifier un identifiant de matche !
  </span>
</div>
EOS;
		return $ans;
		}
		
		try {
			$aMatche = Matche::read($id);
		}
		catch(Exception $e) {
			$ans = <<<EOS
<h1> Une erreur est survenue </h1>
<div align="center">
  <span class="error">
  	Le matche que vous demandez n'existe pas !
  </span>
</div>
EOS;
		return $ans;
		}
		
  	if ($action == "edit")
  		return $this->editMatche($aMatche,$id);
  	else if ($action == "delete")
  		return $this->deleteMatche($aMatche,$id);
  	else
  		return $this->displayForm($aMatche,$id);
	}
	
	
	private function deleteMatche($aMatche,$id) {
		$overview = $aMatche->displayShort();
		
		$teamId = $aMatche->getTeamId();
		$aTeam = Team::read($teamId);
		$aTeam->delMatche($id);
		$aTeam->update();
		
		$aMatche->delete();
		$page = <<<EOS
<h1> Suppression d'un matche </h1>
$overview
<br />
<p> Ce matche a été supprimé avec succès de la base de données </p>
EOS;
		return $page;
	}
	
	
	private function editMatche($aMatche,$id) {

		$check = $this->checkErrors();
		if ($check !== False)
			return $this->displayForm($aMatche,$id,$check);

		
		$matcheNo   = PropRetriever::getProp($_POST,"matcheNo"); 

		$webNo      = PropRetriever::getProp($_POST,"webNo");

		$day        = PropRetriever::getProp($_POST,"day");
		$matcheDate = PropRetriever::getProp($_POST,"matcheDate");
		$length     = PropRetriever::getProp($_POST,"length");
		$opponent   = 
						ucwords(strtolower(PropRetriever::getProp($_POST,"opponent")));
		$goalsUs    = PropRetriever::getInt($_POST,"goalsUs");
		$goalsThem  = PropRetriever::getInt($_POST,"goalsThem");
		
		$teamId    = PropRetriever::getProp($_POST,"teamId");
		$type      = PropRetriever::getProp($_POST,"type");
		$direction = PropRetriever::getProp($_POST,"direction");
	
		$teamsList     = Team::getTeamsAsList($teamId);
		$typeList      = Matche::gettypeAsList($type);
		$directionList = Matche::getDirectionAsList($direction);
		
		// Instanciation du matche
		$aMatche->setMatchNo($matcheNo);
		$aMatche->setMatchWebNo($webNo);
		$aMatche->setGoalsUs($goalsUs);
		$aMatche->setGoalsThem($goalsThem);
		$aMatche->setOpponent($opponent);
		$aMatche->setDirection($direction);
		$aMatche->setLength($length);
		$aMatche->setDay($day);
		$aMatche->setDate($matcheDate);
		$aMatche->setType($type);
		
		$oldTeamId = $aMatche->getTeamId();
		if ($oldTeamId != $teamId) {
			// On efface le lien de l'ancienne équipe
			$oldTeam = Team::read($oldTeamId);
			$oldTeam->delMatche($id);
			$oldTeam->update();
			// On rajoute un lein dans la nouvelle équipe
			$newTeam = Team::read($teamId);
			$newTeam->addMatche($id);
			$newTeam->update();
		}
		
		// On effectue les modifications
		$aMatche->update();
		
		// On signale
		return $this->displayForm($aMatche,$id,array(),
															"Modification enregistrée avec succès");

	}
	
	private function checkErrors() {
		$matcheDate = PropRetriever::getProp($_POST,"matcheDate");
		$length     = PropRetriever::getProp($_POST,"length");
		$opponent   = PropRetriever::getProp($_POST,"opponent");
		$goalsUs    = PropRetriever::getInt($_POST,"goalsUs");
		$goalsThem  = PropRetriever::getInt($_POST,"goalsThem");
		
		$errors = array();
		
		if ($matcheDate == "")
			$errors["matcheDate"] = "La date du matche ne peut être vide !";
		else if (! Date::isValidDate($matcheDate))
   	  $errors["matcheDate"] = 
   	  									"La date du matche n'est pas au format [AAAA-MM-JJ] !";
   	
   	if ($opponent == "")
   		$errors["opponent"] = "Vous devez spécifier un adversaire !";
   	
   	if ($goalsUs == "")
   	 	$errors["goalsUs"] = "Le score de l'équipe ne peut être vide !";
   	else if (! is_numeric($goalsUs))
   	 	$errors["goalsUs"] = 
   	 									"Le score de l'équipe n'est pas une valeur numérique !";
		if ($goalsThem == "")
   	 	$errors["goalsThem"] = "Le score de l'adversaire ne peut être vide !";
   	else if (! is_numeric($goalsThem))
   	 	$errors["goalsThem"] = 
   	 							"Le score de l'adversaire n'est pas une valeur numérique !";
		
		if (count($errors) == 0)
			return False;
		else
			return $errors;
	}
	
	private function displayForm($aMatche, $id,$errors = array(),$text="") {
		$season = Season::read(Login::seasonChoosen(False));
		$season = $season->displayShort();
		
		$matcheNo   = PropRetriever::getProp($_POST,"matcheNo"); 
		if ($matcheNo == "")
			$matcheNo = $aMatche->getMatcheNo();

		$webNo      = PropRetriever::getProp($_POST,"webNo");
		if ($webNo == "")
			$webNo = $aMatche->getMatcheWebNo();
			
		$day        = PropRetriever::getProp($_POST,"day");
		if ($day == "")
			$day = $aMatche->getDay();
			
		$matcheDate = PropRetriever::getProp($_POST,"matcheDate");
		if ($matcheDate == "")
			$matcheDate = $aMatche->getDate();
			
		$length     = PropRetriever::getProp($_POST,"length");
		if ($length == "")
			$length = $aMatche->getLength();
			
		$opponent   = 
							ucwords(strtolower(PropRetriever::getProp($_POST,"opponent")));
		if ($opponent == "")
			$opponent = $aMatche->getOpponent();

		$goalsUs    = PropRetriever::getInt($_POST,"goalsUs");
		if ($goalsUs == 0)
			$goalsUs = $aMatche->getGoalsUs();
			
		$goalsThem  = PropRetriever::getInt($_POST,"goalsThem");
		if ($goalsThem == 0)
			$goalsThem = $aMatche->getGoalsThem();
		
		$teamId    = PropRetriever::getProp($_POST,"teamId");
		if ($teamId == "")
			$teamId = $aMatche->getTeamId();
			
		$type      = PropRetriever::getProp($_POST,"type");
		if ($type == "")
			$type = $aMatche->getType();
			
		$direction = PropRetriever::getProp($_POST,"direction");
		if ($direction == "")
			$direction = $aMatche->getDirection();
	
		$teamsList     = Team::getTeamsAsList($teamId);
		$typeList      = Matche::gettypeAsList($type);
		$directionList = Matche::getDirectionAsList($direction);
	
		$style = array();
		$style["matcheDate"] = ""; $style["length"] = "";
		$style["opponent"] = "";   $style["goalsUs"] = "";
		$style["goalsThem"] = "";
	
		$errorsText = "";
		if (count($errors) != 0) {
			$errorsText = "<h2> Des erreurs sont survenues ! </h2>";
			$errorsText .= "<p> Veuillez corriger les erreurs suivantes : ";
			$errorsText .= "<ul>";
		}
		
		$errorsArray = array();
		foreach($errors as $k => $v) {
			$style[$k] = "class=\"error\"";
			$errorsText .= "<li> <span class=\"error\"> $v </span> </li>";
		}
			
		if ($errorsText != "")
			$errorsText .= "</ul> </p>";
		
		$notification = "";
		if ($text != "") {
			$notification = <<<EOS
<h2> Notification </h2>
<div align="center">
  <span class="error"> $text </span>
</div>
EOS;
		}
		
		$page = <<<EOS
<h1>Étape 3 / 3 : Modification de la Feuille de Match </h1>

<div align="center">
<br /> <br />
<form method="post" accept-charset="utf-8" name="addMatche"
			action="?edit" >

<input type="hidden" name="action" value="edit" />
<input type="hidden" name="id" value="${id}" />
<table> 

<tr> 
<td align="center">

	<fieldset>
		<legend> Informations Administratives </legend>
		<br />
		
		
		<label> 
			<span>N° de Match : </span>
			<input type="text" name="matcheNo" class="edit" value="${matcheNo}"/>
			&nbsp; &nbsp;
		</label>
		<br />		
		
		<label> 
			<span>Minitel / Web : </span>
			<input type="text" name="webNo" class="edit" value="${webNo}"/>
			&nbsp; &nbsp;
		</label>
		<br />
		
		<label> 
			<span> Journée : </span>
			<input type="text" name="day" class="edit" value="${day}"/>
			&nbsp; &nbsp;
		</label>
		<br />
		
		<label> 
			<span ${style["matcheDate"]}> Date : </span>
			<input type="text" name="matcheDate" class="edit" value="${matcheDate}"
						 onclick="javascript:showCal('MatcheDateCal')" />
			&nbsp; &nbsp;
		</label>
		<br />
		
		<label> 
			<span ${style["length"]}> Durée : </span>
			<input type="text" name="length" class="edit" value="${length}"/>
			&nbsp; &nbsp;
		</label>
		<br />

		<br />
	</fieldset>
	
</td>

<td> &nbsp;&nbsp;&nbsp;&nbsp; </td>

<td align="center">
  <fieldset>
		<legend> Informations Techniques </legend>
		<br />
		
		<label> 
			<span>Équipe : </span>
			<select name="teamId" class="edit" />
			${teamsList}
			</select>
			&nbsp; &nbsp;
		</label>
		<br />
		
		<label> 
			<span ${style["opponent"]}>Adversaire : </span>
			<input type="text" name="opponent" class="edit" value="${opponent}"/>
			&nbsp; &nbsp;
		</label>
		<br />
		
		
		<label> 
			<span>Sens : </span>
			<select name="direction" class="edit" />
			${directionList}
			</select>
			&nbsp; &nbsp;
		</label>
		<br />
		
		<label> 
			<span>Type : </span>
			<select name="type" class="edit" />
			${typeList}
			</select>
			&nbsp; &nbsp;
		</label>
		<br />
		
		<label> 
			<span ${style["goalsUs"]}>Score Club : </span>
			<input type="text" name="goalsUs" class="edit" value="${goalsUs}"/>
			&nbsp; &nbsp;
		</label>
		<br />

		<label> 
			<span ${style["goalsThem"]}>Score Adv. : </span>
			<input type="text" name="goalsThem" class="edit" value="${goalsThem}"/>
			&nbsp; &nbsp;
		</label>
		<br />

		<br /> 
	</fieldset>
</td>
</tr>

<tr>
  <td align="center">
  	<br />
    <input type="submit" value="Enregistrer les modifications" class="edit" />
  </td>
  <td align="center">
  	<br />
  </td>
</form>
<form method="post">
<input type="hidden" name="action" value="delete" />
<input type="hidden" name="id" value="${id}" />
  <td align="center">
  	<br />
    <input type="submit" value="Supprimer cette Feuille de Match" 
    			 class="edit" onclick="return confirm('Attention, la suppression d\'une Feuille de Match est définitive ! \\n Il est impossible de revenir en arrière ... \\n Êtes vous sur de vouloir supprimmer cette Feuille de Match ? \\n cliquez sur [OK] pour confirmer votre choix, ou sur [Annuler] pour abandonner la suppression.');"/>
  </td>
</tr>
</table>
</div>
</form>

${notification}
${errorsText}
EOS;

	return $page;
	
	}

}

?>